<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_kuaidisys/model')->方法名();
*/
namespace jy_kuaidisys;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function getfeekd($post=array()){
		$post['address']['pcd']['city']=$post['address']['pcd']['city']=='市辖区'?$post['address']['pcd']['province']:$post['address']['pcd']['city'];
		$post['toaddress']['pcd']['city']=$post['toaddress']['pcd']['city']=='市辖区'?$post['toaddress']['pcd']['province']:$post['toaddress']['pcd']['city'];
		$content=array(
			'channelTag'=>'智能',
			'sender'=>$post['address']['name'],
			'senderMobile'=>$post['address']['mobile'],
			'senderProvince'=>$post['address']['pcd']['province'],
			'senderCity'=>$post['address']['pcd']['city'],
			'senderCounty'=>$post['address']['pcd']['district'],
			'senderTown'=>$post['address']['pcd']['street'],
			'senderLocation'=>$post['address']['address'],
			'senderAddress'=>$post['address']['pcd']['province'].' '.$post['address']['pcd']['city'].' '.$post['address']['pcd']['district'].' '.$post['address']['pcd']['street'].' '.$post['address']['address'],
			'receiver'=>$post['toaddress']['name'],
			'receiverMobile'=>$post['toaddress']['mobile'],
			'receiveProvince'=>$post['toaddress']['pcd']['province'],
			'receiveCity'=>$post['toaddress']['pcd']['city'],
			'receiveCounty'=>$post['toaddress']['pcd']['district'],
			'receiveLocation'=>$post['toaddress']['address'],
			'receiveAddress'=>$post['toaddress']['pcd']['province'].' '.$post['toaddress']['pcd']['city'].' '.$post['toaddress']['pcd']['district'].' '.$post['toaddress']['pcd']['street'].' '.$post['toaddress']['address'],
			'weight'=>$post['weight'],
			'packageCount'=>$post['baonum'],
			'insured'=>$post['baoprice']?:0,
		);
		$result=$this->model('api')->getfeekd($content);
		$setaddfee=$this->setting['addfee']?:0;
		$setaddxufee=$this->setting['addxufee']?:0;
		$xuweight=$post['weight']-1;
		$xuweight=$xuweight>0?$xuweight:0;
		foreach($result as &$item){
			$addfee=$setaddfee;
			if(substr($setaddfee,-1)=='%'){
				$addfee=($item['freight']*substr($setaddfee,0,-1))/100;
				$addfee=intval($addfee*100)/100;
			}
			$addxufee=0;
			if($xuweight>0 && $setaddxufee>0){
				$addxufee=$xuweight*$setaddxufee;
			}
			//报价费
			$item['freightInsured']=$item['freightInsured']?:0;
			$item['endfee']=$item['freight']+$item['freightInsured']+$addfee+$addxufee;
			$item['endfee']=intval($item['endfee']*100)/100;
		}
		paixu($result,'endfee');
		return $result;
	}
	public function getfee($post=array()){
		$post['address']['pcd']['city']=$post['address']['pcd']['city']=='市辖区'?$post['address']['pcd']['province']:$post['address']['pcd']['city'];
		$post['toaddress']['pcd']['city']=$post['toaddress']['pcd']['city']=='市辖区'?$post['toaddress']['pcd']['province']:$post['toaddress']['pcd']['city'];
		$content=array(
			'senderProvince'=>$post['address']['pcd']['province'],
			'senderCity'=>$post['address']['pcd']['city'],
			'senderCounty'=>$post['address']['pcd']['district'],
			'receiveProvince'=>$post['toaddress']['pcd']['province'],
			'receiveCity'=>$post['toaddress']['pcd']['city'],
			'receiveCounty'=>$post['toaddress']['pcd']['district'],
			'calWeight'=>$post['weight'],
			'billType'=>$post['billtype'],
		);
		if($content['billType']==6){
			$content['subType']='showPriceOneAndMore';
		}
		if($content['billType']==2){
			$content['channelType']='biaozhun';
		}
		$fee=5;
		$fee=$this->model('api')->getprice($content);
		$setaddfee=$this->setting['addfee']?:0;
		$setaddxufee=$this->setting['addxufee']?:0;
		$xuweight=$post['weight']-1;
		$xuweight=$xuweight>0?$xuweight:0;
		if($setaddfee){
			$addfee=$setaddfee;
			if(substr($setaddfee,-1)=='%' && $fee){
				$addfee =($fee*substr($setaddfee,0,-1))/100;
				$addfee=intval($addfee*100)/100;
			}
			$fee +=$addfee;
		}
		if($xuweight>0 && $setaddxufee>0){
			$addxufee=$xuweight*$setaddxufee;
			$fee +=$addxufee;
		}
		$fee=intval($fee*100)/100;
		return $fee;
	}
	public function createlog($params=array()){
		if(!$params['express_id']){
			exi('请选择快递公司！','error');
		}
		if(!$params['money']){
			exi('支付金额必须大于零！','error');
		}
		if($this->setting['yufee']>0){
			$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
			if($this->setting['yufee']>$member['credit2']){
				exi('账号余额必须大于，预存款￥'.$this->setting['yufee'].',才能下单！','error','core/member.recharge/');
			}
		}
		if(!$params['address'] || !$params['address']['pcd'] || !$params['address']['address']){
			exi('寄件人地址不完整！','error');
		}
		if(!$params['address']['pcd']['province'] || !$params['address']['pcd']['city'] || !$params['address']['pcd']['district']){
			exi('寄件人地址不完整！','error');
		}
		if(!$params['toaddress'] || !$params['toaddress']['pcd'] || !$params['toaddress']['address']){
			exi('收件人地址不完整！','error');
		}
		if(!$params['toaddress']['pcd']['province'] || !$params['toaddress']['pcd']['city'] || !$params['toaddress']['pcd']['district']){
			exi('收件人地址不完整！','error');
		}
		if($params['address']['pcd']['city']==$params['toaddress']['pcd']['city'] && $params['address']['pcd']['district']==$params['toaddress']['pcd']['district'] && $params['address']['address']==$params['toaddress']['address']){
			exi('收件人地址跟寄件人地址一样？','error');
		}
		$ordersn=$params['ordersn']?:'SN'.get_ordersn($this->tablename('jilog'),'ordersn');
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'address'=>ijson_encode($params['address']),
				'toaddress'=>ijson_encode($params['toaddress']),
				'ordersn'=>$ordersn,
				'money'=>$params['money'],
				'express_id'=>$params['express_id'],
				'expresssn'=>'',
				'typetitle'=>$params['typetitle'],
				'weight'=>$params['weight'],
				'remark'=>$params['remark'],
				'baonum'=>$params['baonum'],
				'baoprice'=>$params['baoprice'],
				'result'=>'',
				'priceinfo'=>ijson_encode($params['priceinfo']),
				'createtime'=>TIMESTAMP,
				'status'=>0,
		);
		$this->insert('jilog',$data);
		$order_id=$this->pdo_insertid();
		return $order_id;
	}
	public function jilog_pay($order_id,$params=array()){
		$update=array(
			'paytype'=>$params['paytype'],
			'transid'=>$params['transid'],
			'ispay'=>1,
			'pay_time'=>TIMESTAMP,
			'status'=>1,
		);
		pdo_update('jy_kuaidisys_jilog',$update,array('id'=>$order_id));
		$order=pdo_get('jy_kuaidisys_jilog',array('id'=>$order_id));
		$order['address']=json_decode($order['address'],true);
		$order['toaddress']=json_decode($order['toaddress'],true);
		$order['address']['pcd']['city']=$order['address']['pcd']['city']=='市辖区'?$order['address']['pcd']['province']:$order['address']['pcd']['city'];
		$order['toaddress']['pcd']['city']=$order['toaddress']['pcd']['city']=='市辖区'?$order['toaddress']['pcd']['province']:$order['toaddress']['pcd']['city'];
		$content=array(
			'channelId'=>$order['express_id'],
			'channelTag'=>'智能',
			'sender'=>$order['address']['name'],
			'senderMobile'=>$order['address']['mobile'],
			'senderProvince'=>$order['address']['pcd']['province'],
			'senderCity'=>$order['address']['pcd']['city'],
			'senderCounty'=>$order['address']['pcd']['district'],
			'senderTown'=>$order['address']['pcd']['street'],
			'senderLocation'=>$order['address']['address'],
			'senderAddress'=>$order['address']['pcd']['province'].' '.$order['address']['pcd']['city'].' '.$order['address']['pcd']['district'].' '.$order['address']['pcd']['street'].' '.$order['address']['address'],
			'dewuAddressId'=>'',
			'receiver'=>$order['toaddress']['name'],
			'receiverMobile'=>$order['toaddress']['mobile'],
			'receiveProvince'=>$order['toaddress']['pcd']['province'],
			'receiveCity'=>$order['toaddress']['pcd']['city'],
			'receiveCounty'=>$order['toaddress']['pcd']['district'],
			'receiveLocation'=>$order['toaddress']['address'],
			'receiveAddress'=>$order['toaddress']['pcd']['province'].' '.$order['toaddress']['pcd']['city'].' '.$order['toaddress']['pcd']['district'].' '.$order['toaddress']['pcd']['street'].' '.$order['toaddress']['address'],
			'weight'=>$order['weight'],
			'packageCount'=>$order['baonum'],
			'itemName'=>$order['typetitle'],
			'insured'=>$order['baoprice'],
			'billRemark'=>$order['remark'],
		);
		$status=2;
		$res=$this->model('api')->addorder($content);
		if($res['code']!=1){
			$status=5;
		}
		$update=array(
			'result'=>ijson_encode($res['result']),
			'expresssn'=>$res['result']['waybill'],
			'koufee'=>$res['result']['freight'],
			'channel'=>$res['result']['channel'],
			'status'=>$status,
		);
		if($res['code']!=1){
			$update['code_msg']=$res['message'];
		}
		pdo_update('jy_kuaidisys_jilog',$update,array('id'=>$order_id));
		if($res['result']['freight']>0){
			$order['koufee']=$res['result']['freight'];
			$order['status']=$status;
			$this->model('commission')->create($order);
			$this->model('commission')->jy_area_agent($order);
		}
		return true;
	}
	public function jilog_cancel($order_id){
		$order=$this->pdo_get('jilog',array('id'=>$order_id));
		if($order['status']){
			exi('该订单不支持取消！','error');
		}
		$update=array(
			'status'=>4,
		);
		pdo_update('jy_kuaidisys_jilog',$update,array('id'=>$order_id));
		return true;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
}
?>